<template>
  <div class="favorites-page">
    <TopNav title="我的收藏"/>

    <main class="main-content">
      <div v-if="favoriteRecipes.length === 0" class="empty-state">
        <Heart :size="64" class="empty-icon"/>
        <h3>还没有收藏的食谱</h3>
        <p>去首页发现更多美味吧！</p>
        <button @click="goToHome" class="btn btn-primary">去首页看看</button>
      </div>

      <div v-else class="favorites-grid">
        <RecipeCard
            v-for="recipe in favoriteRecipes"
            :key="recipe.id"
            :recipe="recipe"
            @favorite-toggle="toggleFavorite"
        />
      </div>
    </main>
  </div>
</template>

<script setup lang="ts">
import {onMounted, ref} from 'vue'
import {useRouter} from 'vue-router'
import {Heart} from 'lucide-vue-next'
import TopNav from '@/components/TopNav.vue'
import RecipeCard from '@/components/RecipeCard.vue'

interface Recipe {
  id: string
  title: string
  description: string
  image: string
  cookTime: number
  rating: number
  isFavorite: boolean
}

const router = useRouter()

const favoriteRecipes = ref<Recipe[]>([])

const mockRecipes: Recipe[] = [
  {
    id: '2',
    title: '宫保鸡丁',
    description: '川菜经典，麻辣鲜香，下饭神器',
    image: 'https://images.unsplash.com/photo-1563379091339-03246963d4d0?w=400',
    cookTime: 30,
    rating: 4.6,
    isFavorite: true
  },
  {
    id: '4',
    title: '麻婆豆腐',
    description: '四川名菜，麻辣鲜香，嫩滑爽口',
    image: 'https://images.unsplash.com/photo-1563379091339-03246963d4d0?w=400',
    cookTime: 20,
    rating: 4.5,
    isFavorite: true
  }
]

const toggleFavorite = (recipeId: string) => {
  const recipe = favoriteRecipes.value.find(r => r.id === recipeId)
  if (recipe) {
    recipe.isFavorite = false
    // 从收藏列表中移除
    favoriteRecipes.value = favoriteRecipes.value.filter(r => r.id !== recipeId)
  }
}

const goToHome = () => {
  router.push('/')
}

onMounted(() => {
  favoriteRecipes.value = mockRecipes.filter(recipe => recipe.isFavorite)
})
</script>

<style lang="scss" scoped>
.favorites-page {
  min-height: 100vh;
  background-color: var(--color-background);
}

.main-content {
  padding: var(--spacing-md);
  max-width: 1200px;
  margin: 0 auto;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--spacing-xl) var(--spacing-md);
  text-align: center;

  .empty-icon {
    color: var(--color-text-light);
    margin-bottom: var(--spacing-lg);
  }

  h3 {
    font-size: var(--font-size-xl);
    color: var(--color-text);
    margin-bottom: var(--spacing-sm);
  }

  p {
    color: var(--color-text-light);
    margin-bottom: var(--spacing-lg);
  }
}

.favorites-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: var(--spacing-lg);
}

@media (max-width: 768px) {
  .favorites-grid {
    grid-template-columns: 1fr;
    gap: var(--spacing-md);
  }
}
</style>
